﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using map.search;
using RuleCode.Table;

namespace chatblog
{
    public class Util
    {
        /// <summary>
        /// 验证是否为管理员账户
        /// </summary>
        /// <returns></returns>
        public static  bool ValidateAdmin()
        {         
            bool result = false;
            List<string> adminList = new List<string>();
            adminList.AddRange(new string[] { "dingyuliang@rulecode.com", "maxfara@163.com" });
            if (adminList.Contains(AliApi.appUserId))
                result = true;
            return result;
        }
        public static void CheckBrokerOpr(Page page)
        {
            if ((AliApi.brokerOpr == "2"))
            {
                string url = @"~/default.aspx";
                HttpContext.Current.Response.Redirect(page.ResolveUrl(url));
            }
        }

        public static  string UploadImageDirectory
        {
            get
            {
                return @"~/upload/images/";
            }
        }

        /// <summary>
        /// 跳转到替换页面
        /// </summary>
        /// <returns></returns>
        public static void RedirectReplacePage(Page page,string reason,string redirectUrl)
        {
            string url = "~/replace.htm?Reason=" + reason + "&RedirectUrl=" + redirectUrl;
            HttpContext.Current.Response.Redirect(page.ResolveUrl (url));
        }

        /// <summary>
        /// 判断窗体是刷新状态还是关闭
        /// </summary>
        public static  bool isFlush
        {
            get
            {
                if (HttpContext.Current.Session["isFlush"] == null)
                    return false;
                return Convert.ToBoolean(HttpContext.Current.Session["isFlush"]);
            }
            set
            {
                HttpContext.Current.Session["isFlush"] = value;
            }
        }

        private static Dictionary<ChatUser, ChatUser> chatUserPairs; 
        public static Dictionary<ChatUser, ChatUser> ChatUserPairs
        {
            get
            {
                if (chatUserPairs == null)
                    chatUserPairs = new Dictionary<ChatUser, ChatUser>();
                return chatUserPairs;
            }
        }


        /// <summary>
        /// 获取或设置 当用户退出的时候是否要改变聊天室状态
        /// </summary>
        public static  bool useLoginOut
        {
            get
            {
                bool _useloginout = true;
                if (HttpContext.Current.Session["useLoginOut"] != null)
                {
                    try
                    {
                        _useloginout = Convert.ToBoolean(HttpContext.Current.Session["useLoginOut"]);
                    }
                    catch
                    {
                        _useloginout = true;
                    }
                }
                return _useloginout;
            }
            set
            {
                HttpContext.Current.Session["useLoginOut"] = value;
            }
        }
        public static string bind1()
        {

            DataTable a1;
            string sql = "";
            sql = " select recid,区编码 from varea3 where 省编码 =  '"
                  + map.zfunction.Province + "'";
            string province = "";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];


            string s;
            s = "<div style =\"width:100%; \" class=\"style_t1\" >";

            s += "<div id=\"area3\" style =\"width:100%;\"><a href=\"#\" onclick=\"selall()\" >不限</a>";
            foreach (DataRow item in a1.Rows)
            {
                s += "&nbsp;<a href=\"javascript:;\" onclick=\"selarea1('" + item["区编码"] + "')\">" + item["区编码"] +
                     "</a>&nbsp;";
            }
            s += "</div>";
           // s += "<hr />";

            s += "<div id=\"area4\" style =\"width:100%;\" class=\"style_t2\">";
            //s += "请先选择区域.";
            s += "</div>";

            s += "</div>";
            return s;
        }



        #region add by dingyuliang

        /// <summary>
        /// 获取或设置 所有打开的聊天室窗口列表管理对象
        /// </summary>
        public static  List<cbChatRoom_Struct> ChatRoomStructList
        {
            get 
            {
                if (HttpContext.Current.Session["ChatRoomStructList"] == null)
                    HttpContext.Current.Session["ChatRoomStructList"] = new List<cbChatRoom_Struct>();
                return (List<cbChatRoom_Struct>)HttpContext.Current.Session["ChatRoomStructList"];
            }
            set
            {
                HttpContext.Current.Session["ChatRoomStructList"] = value;
            }
        }

        /// <summary>
        /// 移除聊天窗口
        /// </summary>
        /// <param name="user_to"></param>
        /// <param name="user_from"></param>
        public static void RemoveChatRoom(string user_to, string user_from)
        {
            if (ChatRoomStructList != null && ChatRoomStructList.Count > 0)
            {
                int length = ChatRoomStructList.Count ;
                for (int i = 0; i < length;i++ )
                {
                    cbChatRoom_Struct chatRoom_Struct = ChatRoomStructList[i];
                    if (chatRoom_Struct.user_from == user_from && chatRoom_Struct.user_to == user_to)
                    {
                        ChatRoomStructList.Remove(chatRoom_Struct);
                    }
                }
            }
        }

        /// <summary>
        /// 获取聊天室对象
        /// </summary>
        /// <param name="user_to"></param>
        /// <param name="user_from"></param>
        /// <returns></returns>
        public static cbChatRoom_Struct GetChatRoom(string user_to, string user_from)
        {
            if (ChatRoomStructList != null && ChatRoomStructList.Count > 0)
            {
                int length = ChatRoomStructList.Count;
                for (int i = 0; i < length; i++)
                {
                    cbChatRoom_Struct chatRoom_Struct = ChatRoomStructList[i];
                    if (chatRoom_Struct.user_from == user_from && chatRoom_Struct.user_to == user_to)
                    {
                        return  ChatRoomStructList[i];
                    }
                }

            }
            return  new cbChatRoom_Struct ();
        }

        public static void AddNewChatRoom(cbChatRoom_Struct chatRoom_struct)
        { 
            
        }


        /// <summary>
        /// 判断是否为当前用户打开聊天室
        /// </summary>
        /// <param name="_currentUser"></param>
        /// <returns></returns>
        public static bool ChatRoomIsOpenByCurrentUser(cbuser _currentUser)
        {
            bool result = false;
            if (_currentUser.Count > 0)
            {
                string chatAddress = _currentUser.cbChatAddress.Value;
                string[] chatAddressResult = new string[3];
                string[] arg0 = chatAddress.Split(";".ToCharArray());
                if (arg0.Length >= 3)
                {
                    string[] ipArgs = arg0[0].Split("=".ToCharArray());
                    chatAddressResult[0] = ipArgs[1];
                    string[] timeArgs = arg0[1].Split("=".ToCharArray());
                    chatAddressResult[1] = timeArgs[1];
                    string[] hostArgs = arg0[2].Split("=".ToCharArray());
                    chatAddressResult[2] = hostArgs[1];
                }

                //if(chatAddressResult[0]!=string.Empty && chatAddressResult[0]
            }
            return result;
        }

        /// <summary>
        /// 判断是否为当前用户打开聊天室
        /// </summary>
        /// <param name="_currentUser"></param>
        /// <returns></returns>
        public static bool ChatRoomIsOpenByCurrentUser()
        {
            bool result = false;
            return result;
        }
        #endregion

    }
    

    /// <summary>
    /// chat userInfo 
    /// </summary>
    public struct ChatUser
    {
        /// <summary>
        /// 
        /// </summary>
        public string UserId
        { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public bool isOnline 
        { get; set; }


        /// <summary>
        /// 
        /// </summary>
        public ChatUserRole Role
        { get; set; }
    }

    public enum ChatUserRole
    {
        Sender =0,

        Receiver 
    }
}
